import { getBaseUrl } from '@/services/urlconst';
import {
  getCompanyID,
  getPosIDs,
  getUserDepID,
  getUserID,
  requestFunComponFiex,
} from '@/utils';
import { message } from 'antd';
import { useMemo } from 'react';
import { useRequest } from 'umi';
let baseUrl = getBaseUrl();
// let baseUrl = 'http://192.168.3.21:9001'; //小田本地

/**
 * 置顶
 * @param issueAnnounceCallllback 接口调用后的回调
 * @returns
 */
const issueAnnounceService = (issueAnnounceCallllback: (res: any) => void) => {
  const issueAnnounce = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/announceController/getIssueAnnounceList`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: issueAnnounceCallllback,
    },
  );
  const dataList = useMemo(() => {
    return issueAnnounce?.data?.rows;
  }, [issueAnnounce?.data]);
  return {
    issueAnnounceRun: issueAnnounce?.run,
    issueAnnounceLoading: issueAnnounce?.loading,
    issueAnnounceData: dataList,
  };
};

/**
 * 获取详情
 * @returns
 */
const getAnnounceByIDService = () => {
  const getAnnounceInfo = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/announceController/getAnnounceByID`;
      let bodyParams = {
        id: params?.id,
        view: {
          viewerID: getUserID(),
          viewPositionID: getPosIDs(),
          viewDepartmentID: getUserDepID(),
          viewCompanyID: getCompanyID(),
        },
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (res && res?.msgFlag) {
          message.success(res?.msg);
        } else {
          message.error(res?.msg);
        }
      },
    },
  );
  const dataInfoObj = useMemo(() => {
    return getAnnounceInfo?.data?.obj;
  }, [getAnnounceInfo?.data]);

  return {
    getAnnounceInfoRun: getAnnounceInfo?.run,
    getAnnounceLoading: getAnnounceInfo?.loading,
    getAnnounceInfoObj: dataInfoObj,
  };
};

export { issueAnnounceService, getAnnounceByIDService };
